package com.whynot.frame.starter.auto;

import com.whynot.frame.starter.model.HelloModel;
import com.whynot.frame.starter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration  //注入SpringIoc
@ConditionalOnClass(HelloService.class) //判断classpath下是否存在HelloService，有就初始化HelloAutoConfiguration
@EnableConfigurationProperties(HelloModel.class) //将 application.properties 的相关的属性字段与该类一一对应，并生成 Bean
public class HelloAutoConfiguration {
    @Autowired
    HelloModel helloModel;

    @Bean
    @ConditionalOnMissingBean(HelloService.class) // 当容器没有这个 Bean 的时候才创建这个 Bean。防止重复创建
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setName(helloModel.getName());
        helloService.setWord(helloModel.getWord());
        return helloService;
    }
}
